﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using UnitySheet;

public class SetCell : MonoBehaviour {
    public Sprite textImage,dropDownImage;
    public InputField descriptionInput;
    public Dropdown dropDown;
    bool isText = false;
    bool isSame = false;
    int inputListenerCount = 0;
    // Use this for initialization
    void Start()
    {
        SheetManager.sheetManager.sheetOperator.onSelectCell += OnSelectedCellsChanged;
        SheetManager.sheetManager.sheetOperator.onDeselectCell += OnSelectedCellsChanged;
        descriptionInput.onValueChanged.AddListener(SetCellDescription);
    }
	
	// Update is called once per frame
	void Update () {
		
	}

    //public void AddInputFieldListener(UnityAction<string> call)
    //{
    //    descriptionInput.onValueChanged.AddListener(call);
    //    inputListenerCount++;
    //    if (inputListenerCount > 0)
    //    {
    //        SetDescriptionInputInteractable(true);
    //    }
    //}

    //public void RemoveInputFieldListener(UnityAction<string> call)
    //{
    //    descriptionInput.onValueChanged.RemoveListener(call);
    //    if (inputListenerCount == 0)
    //    {
    //        SetDescriptionInputInteractable(false);
    //    }
    //}

    /// <summary>
    /// 设置单元格内容
    /// </summary>
    /// <param name="cellContent"></param>
    public void SetCellContent(int cellContent)
    {
        Cell[] cells = SheetManager.sheetManager.sheetOperator.GetSelectedCells();
        Sprite icon;
        switch (cellContent)
        {
            case 1:
                SetDescriptionInputInteractable(true);
                icon = textImage;
                break;
            case 2:
                SetDescriptionInputInteractable(false);
                icon = dropDownImage;
                break;
            default:
                SetDescriptionInputInteractable(false);
                icon = null;
                break;
        }
        foreach (var item in cells)
        {
            CellContent content;
            switch (cellContent)
            {
                case 1:
                    content = new TextCellContent();
                    ((ContentCell)item).SetContent(content);
                    break;
                case 2:
                    content = new OptionCellContent();
                    ((ContentCell)item).SetContent(content);
                    break;
                default:
                    break;
            }
        }
    }

    public void SetCellDescription(string description)
    {
        Cell[] cells = SheetManager.sheetManager.sheetOperator.GetSelectedCells();
        foreach (var item in cells)
        {
            if (item is ContentCell && ((ContentCell)item).cellContent != null && ((ContentCell)item).cellContent.contentType == CellContent.CellContentType.Text)
            {
                ((ContentCell)item).cellContent.description = description;
            }
        }
    }

    /// <summary>
    /// 当选择的单元格发生改变，判断单元格设置类型和当单元格为文字类型时输入框的状态
    /// </summary>
    /// <param name="cell"></param>
    public void OnSelectedCellsChanged(Cell cell)
    {
        Cell[] cells = SheetManager.sheetManager.sheetOperator.GetSelectedCells();
        isText = false;
        isSame = false;
        CellContent.CellContentType contentType = CellContent.CellContentType.N;
        if (cells.Length > 0)
        {
            dropDown.interactable = true;
            if (((ContentCell)cells[0]).cellContent != null)
            {
                contentType = ((ContentCell)cells[0]).cellContent.contentType;
            }
            else
            {
                contentType = CellContent.CellContentType.N;
            }
            isText = contentType == CellContent.CellContentType.Text;
            isSame = true;
            bool textSame = true;
            string s0 = isText ? ((ContentCell)cells[0]).cellContent.description : string.Empty;
            for (int i = 1; i < cells.Length; i++)
            {
                CellContent.CellContentType ct = ((ContentCell)cells[i]).cellContent == null ? CellContent.CellContentType.N : ((ContentCell)cells[i]).cellContent.contentType;
                if (isSame)
                {
                    isSame = ct == contentType;
                }
                else
                {
                    break;
                }
                if (isText)
                {
                    textSame = textSame && ((ContentCell)cells[i]).cellContent.description == s0;
                   
                    isText = isSame && contentType == CellContent.CellContentType.Text;
                }
            }
            if (isSame)
            {
                dropDown.value = (int)contentType;
                dropDown.captionText.text = dropDown.options[(int)contentType].text;
            }
            else
            {
                dropDown.value = 0;
            }
            SetDescriptionInputInteractable(isText);
            if (textSame)
            {
                descriptionInput.text = s0;
            }
        }
        else
        {
            dropDown.value = 0;
            dropDown.interactable = false;
            SetDescriptionInputInteractable(false);
        }
    }
    void SetDescriptionInputInteractable(bool b)
    {
        descriptionInput.interactable = b;
        if (!b)
        {
            descriptionInput.text = string.Empty;
        }
    }

}
